package com.open.party.cloud.view.examine.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewBaseFragment
import com.open.party.cloud.R
import com.open.party.cloud.model.CommVo
import com.open.party.cloud.model.ExamineClassEntity
import com.open.party.cloud.view.examine.ExamineClassExamineListActivity
import com.open.party.cloud.view.examine.ExamineJoinClassActivity
import com.open.party.cloud.view.examine.adapter.ExamineClassForMineListAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.fragment_examine_classes_ex_list_view.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


open class ExamineClassForMineListFragment : AppEventBusRecycleViewBaseFragment<CommVo>(), OnSuperListener<ExamineClassEntity> {

    var viewModel: ExamineViewModel? = null

    private var adapter: ExamineClassForMineListAdapter? = null
    private var currView: View? = null

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.fragment_examine_classes_ex_list_view, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ExamineViewModel()

        initRecycleLinearView(loadingRecyclerView)
        loadingRecyclerView.setLoadingMoreEnabled(false)
        adapter = ExamineClassForMineListAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
        addClassItem.setOnClickListener { addClassByQrCode() }
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    override fun loadData(pageVo: PageVo<CommVo>) {
        val pageReq = PageReq<CommVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val vo = CommVo()
        pageReq.data = vo

//        viewModel!!.getMyClassList(pageReq)

        viewModel!!.getJoinClassList()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<List<ExamineClassEntity>>) {
        if ("getJoinClassList" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null && result.data.isEmpty()) {
                    adapter!!.setData(ArrayList<ExamineClassEntity>())
                    loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                    loadingRecyclerView.setLoadingMoreEnabled(false)
                } else {
                    adapter!!.setData(result.data)
                    loadingTipView.showContent()
                }
            }
            40001 -> {
                loadingTipView.showError("请重新登录后再使用")
            }
            40013 -> {
                loadingTipView.showError("请重新登录后再使用")
            }
            40011 -> {
                loadingTipView.showError("登录过期，请重新登录")
            }

            40015 -> {
                loadingTipView.showError("请重新登录后再使用")
//                XUtils.intent().openActivity(activity, LoginActivity::class.java).start()
            }

            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    override fun onClick(position: Int, itemData: ExamineClassEntity, flag: String?) {
        when (flag) {
            "rootView" -> {
                if ("pass" == itemData.state) {
                    XUtils.intent().openActivity(activity, ExamineClassExamineListActivity::class.java)
                        .putSerializableExtra("examineClass", itemData)
                        .startInFragment(this)
                }
            }

            "joinBtn" -> {
                if ("noJoin" == itemData.state || "back" == itemData.state ) {
                    XUtils.intent().openActivity(activity, ExamineJoinClassActivity::class.java)
                        .putStringExtra("classId", itemData.id)
                        .startInFragment(this)
                }
            }
        }
    }

    private fun addClassByQrCode() {
//        XUtils.intent().openActivity(activity, QrScanActivity::class.java)
//            .requestCode(REQUEST_QR_CODE)
//            .startInFragment(this)
    }

    private val REQUEST_QR_CODE = 500

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != 200) return

        when (requestCode) {
            REQUEST_QR_CODE -> {
                val result = data!!.getStringExtra("value") as String
                if (result.contains("classId://")) {
                    var classId = result.replace("classId://", "")
                    XUtils.intent().openActivity(activity, ExamineJoinClassActivity::class.java)
                        .putStringExtra("classId", classId)
                        .startInFragment(this)
                }
            }
        }
    }
}